package ac.svn.accesscontrol.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.store.UserDetails;
import ac.svn.accesscontrol.ui.IUIConstants;
import ac.svn.accesscontrol.ui.STextField;
import ac.svn.accesscontrol.ui.UIComponentLibrary;
import ac.svn.accesscontrol.ui.WhiteLabel;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

public class UserBasicInfoEditorPanel extends JPanel implements IUIConstants {

	public interface ExportedComponents
	{
		public static final String C_UBI_PANEL						= "C_UBI_PANEL";
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textName, textEMailAdd, textTeam,textSVNUserId;
	private JTextPane textPane;
	
	public UserBasicInfoEditorPanel()
	{
		super(new BorderLayout(5, 5),true);
		setBackground(Color.WHITE);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBackground(Color.WHITE);
		scrollPane.setPreferredSize(new Dimension(2, 150));
		scrollPane.setBorder(UIUtility.setAllShadowedBorder(null));
		
		add(scrollPane, BorderLayout.CENTER);
		
		textPane = new JTextPane();
		Border shadowBorder = UIUtility.setAllShadowedBorder(textPane);
		scrollPane.setViewportView(textPane);
		
		WhiteLabel lblAboutUser = new WhiteLabel("<HTML><h3> About User");
		lblAboutUser.enableShadow(true);
		lblAboutUser.setBorder(shadowBorder);
		
		scrollPane.setColumnHeaderView(lblAboutUser);
		
		JPanel panelNorth = new JPanel();
		add(panelNorth, BorderLayout.NORTH);
		panelNorth.setLayout(new BorderLayout(0, 0));
		
		JPanel pnlImage = new JPanel();
		pnlImage.setBackground(Color.WHITE);
		pnlImage.setBorder(shadowBorder);
		
		panelNorth.add(pnlImage, BorderLayout.WEST);
		JLabel icon = new WhiteLabel(Utility.getImageIcon(IResources.LOC_ICN_USER_DETAILS));
		pnlImage.add(icon);
		
		JPanel pnlInfo = new JPanel();
		pnlInfo.setBackground(Color.WHITE);
		pnlInfo.setBorder(shadowBorder);
		pnlInfo.setLayout(new GridLayout(4, 2, 5, 5));
		
		panelNorth.add(pnlInfo, BorderLayout.CENTER);
		
		JLabel lblName = new JLabel(" Name*");
		lblName.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInfo.add(lblName);
		
		textName = new STextField();
		pnlInfo.add(textName);
		textName.setColumns(10);
		
		JLabel lblEmailAddress = new JLabel(" Email Address");
		lblEmailAddress.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInfo.add(lblEmailAddress);
		
		textEMailAdd = new STextField();
		pnlInfo.add(textEMailAdd);
		textEMailAdd.setColumns(10);
		
		JLabel lblTeam = new JLabel(" Team");
		lblTeam.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInfo.add(lblTeam);
		
		textTeam = new STextField();
		pnlInfo.add(textTeam);
		textTeam.setColumns(10);
		
		JLabel lblEmployeeid = new JLabel(" SVN UserId*");
		lblEmployeeid.setHorizontalAlignment(SwingConstants.LEFT);
		pnlInfo.add(lblEmployeeid);
		
		textSVNUserId = new STextField();
		pnlInfo.add(textSVNUserId);
		textSVNUserId.setColumns(10);
		
		textName.requestFocus();
		
		registerComponents();
	}
	
	private void registerComponents() {
		UIComponentLibrary.addComponent(ExportedComponents.C_UBI_PANEL, this);
	}

	public boolean isDataComplete()
	{
		boolean dataComplete = false;
		if((false == textName.getText().trim().isEmpty())&&(false == textSVNUserId.getText().trim().isEmpty()))
		{
			dataComplete = true; 
		}
		return dataComplete;
	}
	
	public void setData(String strName, String strMailId, String strTeam,String strUserId,String strAbout)
	{
		textName.setText(strName);
		textEMailAdd.setText(strMailId);
		textTeam.setText(strTeam);
		textSVNUserId.setText(strUserId);
		textPane.setText(strAbout);
		textPane.setCaretPosition(0);
	}

	public JTextField getTextName() {
		return textName;
	}

	public JTextField getTextEMailAdd() {
		return textEMailAdd;
	}

	public JTextField getTextTeam() {
		return textTeam;
	}

	public JTextField getTextSVNUserId() {
		return textSVNUserId;
	}

	public JTextPane getTextPane() {
		return textPane;
	}
	
	public void resetData()
	{
		textEMailAdd.setText("");
		textName.setText("");
		textPane.setText("");
		textSVNUserId.setText("");
		textSVNUserId.setBorder(textEMailAdd.getBorder()); 
		textTeam.setText("");
	}
	
	public UserDetails getUserDetails()
	{
		UserDetails userDetails = new UserDetails(textName.getText(), textSVNUserId.getText());
		userDetails.setAboutUser(textPane.getText());
		userDetails.seteMailAddress(textEMailAdd.getText());
		userDetails.setTeam(textTeam.getText());
		return userDetails;
	}
}
